package tcpudp

import (
	"github.com/project-flogo/core/data/coerce"
)

type Settings struct {
	Network   string `md:"network"`       // The network type
	Host      string `md:"host"`          // The host name or IP for TCP server.
	Port      string `md:"port,required"` // The port to listen on
	Delimiter string `md:"delimiter"`
	Max       int    `md:"max"`
	Timeout   string `md:"timeout"`
}

type HandlerSettings struct {
}

type Output struct {
	Data       []byte `md:"data"` // The data received from the connection
	LocalAddr  string `md:"localAddr"`
	RemoteAddr string `md:"remoteAddr"`
}

type Reply struct {
	Reply []byte `md:"reply"` // The reply to be sent back
}

func (o *Output) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"data":       o.Data,
		"localAddr":  o.LocalAddr,
		"remoteAddr": o.RemoteAddr,
	}
}

func (o *Output) FromMap(values map[string]interface{}) error {

	var err error
	o.Data, err = coerce.ToBytes(values["data"])
	if err != nil {
		return err
	}

	o.LocalAddr, err = coerce.ToString(values["localAddr"])
	if err != nil {
		return err
	}

	o.RemoteAddr, err = coerce.ToString(values["remoteAddr"])
	if err != nil {
		return err
	}

	return nil
}

func (r *Reply) ToMap() map[string]interface{} {
	return map[string]interface{}{
		"reply": r.Reply,
	}
}

func (r *Reply) FromMap(values map[string]interface{}) error {

	var err error
	r.Reply, err = coerce.ToBytes(values["reply"])
	if err != nil {
		return err
	}

	return nil
}
